<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * konsepdiri.php
 * @author ranuandra
 *
 * 10 Jul 2012 10:44:32
 *
 * 
 */

class Controller_Konsepdiri extends Kohana_Nasmoco
{
	public function action_index()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$konsepdiri = ORM::factory('mstkonsepdiri')->find_all();
		$view = View::factory('konsepdiri/index');
		$view->pelamar = $pelamar;
		$view->konsepdiri = $konsepdiri;
		$this->template->container = $view;
	}
	
	public function action_edit()
	{
		$konsepdiri = ORM::factory('mstkonsepdiri')->find_all();
	}
	
	public function action_save()
	{
		$db = Database::instance();
		$db->begin();
		
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		
		$datas = Request::current()->post('data');
		foreach($datas as $data)
		{
			try {
				$no = $data['pertanyaan'];
				if(isset($data['jawab'.$no]))
				{
					$penjelasan = $data['penjelasan'.$no];
					$jawab = $data['jawab'.$no];
					if($jawab == "ya")
					{
						$njawab = 1;
					}
					else 
					{
						$njawab = 0;
					}
					
					if(isset($data['id']))
					{
						$konsep = ORM::factory('pelamarkonsepdiri', $data['id']);
						$konsep->penjelasan = $penjelasan;
						$konsep->jawab = $njawab;
						$konsep->created = date("Y-m-d H:i:s");
						$konsep->save();
					}
					else
					{
						$konsep = ORM::factory('pelamarkonsepdiri');
						$konsep->mstpelamar_id = $pelamar->id;
						$konsep->mstkonsepdiri_id = $no;
						$konsep->penjelasan = $penjelasan;
						$konsep->jawab = $njawab;
						$konsep->created = date("Y-m-d H:i:s");
						$konsep->save();					
					}
					
				}
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				print_r($e);
			}

		}
		$db->commit();
		Return Request::current()->redirect(URL::base()."user");
	}
}